package com.xjtu.weixin.builder;

import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutNewsMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutTextMessage;

import java.util.ArrayList;
import java.util.List;

/**
 * 文本消息构建
 * @author Binary Wang(https://github.com/binarywang)
 */
@Slf4j
public class TextBuilder extends AbstractBuilder {

	@Override
	public WxMpXmlOutMessage build(String content, WxMpXmlMessage wxMessage,
	                               WxMpService service) {

		try {
			log.info(service.getUserService().userInfo(wxMessage.getFromUser()).toString());
		} catch (WxErrorException e) {
			e.printStackTrace();
		}
		if ("11".equals(wxMessage.getContent())) {
			List<WxMpXmlOutNewsMessage.Item> itemList = new ArrayList<>();

			WxMpXmlOutNewsMessage.Item item = new WxMpXmlOutNewsMessage.Item();
			WxMpXmlOutNewsMessage.Item item1 = new WxMpXmlOutNewsMessage.Item();
			WxMpXmlOutNewsMessage.Item item2 = new WxMpXmlOutNewsMessage.Item();

			item.setTitle("紧急通知，不要捡这种钱！湛江都已经传疯了！\n");
			item.setDescription("");
			item.setPicUrl("http://www.sinaimg.cn/dy/slidenews/31_img/2016_38/28380_733695_698372.jpg");
			item.setUrl("http://mp.weixin.qq.com/s?__biz=MjM5Njc2OTI4NQ==&mid=2650924309&idx=1&sn=8bb6ae54d6396c1faa9182a96f30b225&chksm=bd117e7f8a66f769dc886d38ca2d4e4e675c55e6a5e01e768b383f5859e09384e485da7bed98&scene=4#wechat_redirect");

			item1.setTitle("湛江谁有这种女儿，请给我来一打！");
			item1.setDescription("");
			item1.setPicUrl("http://www.sinaimg.cn/dy/slidenews/31_img/2016_38/28380_733695_698372.jpg");
			item1.setUrl("http://mp.weixin.qq.com/s?__biz=MjM5Njc2OTI4NQ==&mid=2650924309&idx=2&sn=d7ffc840c7e6d91b0a1c886b16797ee9&chksm=bd117e7f8a66f7698d094c2771a1114853b97dab9c172897c3f9f982eacb6619fba5e6675ea3&scene=4#wechat_redirect");

			item2.setTitle("以上图片我就随意放了");
			item2.setDescription("");
			item2.setPicUrl("http://www.sinaimg.cn/dy/slidenews/31_img/2016_38/28380_733695_698372.jpg");
			item2.setUrl("http://mp.weixin.qq.com/s?__biz=MjM5Njc2OTI4NQ==&mid=2650924309&idx=3&sn=63e13fe558ff0d564c0da313b7bdfce0&chksm=bd117e7f8a66f7693a26853dc65c3e9ef9495235ef6ed6c7796f1b63abf1df599aaf9b33aafa&scene=4#wechat_redirect");
			itemList.add(item);
			itemList.add(item1);
			itemList.add(item2);
			WxMpXmlOutNewsMessage m = WxMpXmlOutMessage.NEWS().articles(itemList)
					.fromUser(wxMessage.getToUser()).toUser(wxMessage.getFromUser())
					.build();
			return m;
		} else {
			WxMpXmlOutTextMessage m = WxMpXmlOutMessage.TEXT().content(content)
					.fromUser(wxMessage.getToUser()).toUser(wxMessage.getFromUser())
					.build();
			return m;
		}
	}

}
